<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>

    <style>
        .article-type-img {
            width: 36px;
            height: 32px;
            line-height: 32px;
        }

        #div_digg {
            float: right;
            margin-bottom: 10px;
            margin-right: 30px;
            font-size: 12px;
            width: 125px;
            text-align: center;
            margin-top: 10px;
        }

        .diggit {
            float: left;
            width: 46px;
            height: 52px;
            background: url(/static/img/upup.gif) no-repeat;
            text-align: center;
            cursor: pointer;
            margin-top: 2px;
            padding-top: 5px;
        }

        .buryit {
            float: right;
            margin-left: 20px;
            width: 46px;
            height: 52px;
            background: url(/static/img/downdown.gif) no-repeat;
            text-align: center;
            cursor: pointer;
            margin-top: 2px;
            padding-top: 5px;
        }

        .clear {
            clear: both;
        }

        .diggword {
            margin-top: 5px;
            margin-left: 0;
            font-size: 12px;
            color: #808080;
        }

        .clearfix:after {
            content: '';
            display: block;
            clear: both
        }

    </style>
</head>
<body>
<nav class="navbar navbar-default .navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/home/">七仔家园</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li class="active"><a href="/home/">博客 <span class="sr-only">(current)</span></a></li>
                <li><a href="#">学堂</a></li>
            </ul>
            <!--<form class="navbar-form navbar-left">
                <div class="form-group">
                    <input type="text" class="form-control" placeholder="Search">
                </div>
                <button type="submit" class="btn btn-default">搜索</button>
            </form>-->

            <ul class="nav navbar-nav navbar-right">
                {% if request.user.is_authenticated %}
                    <li><a href="/mysite/{{ request.user.username }}/">{{ request.user.username }}</a></li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                           aria-expanded="false">更多操作 <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="#" data-toggle="modal" data-target=".bs-example-modal-lg">修改密码</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a href="/user/logout/">退出登录</a></li>
                        </ul>
                    </li>
                    <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog"
                         aria-labelledby="myLargeModalLabel">
                        <div class="modal-dialog modal-lg" role="document">
                            <div class="modal-content">
                                <div class="container-fluid">
                                    <div class="row">
                                        <div class="col-md-8 col-md-offset-2">
                                            <h2 class="text-center">修改密码</h2>
                                            <div class="form-group">
                                                <label for="">用户名:</label>
                                                <input type="text" class="form-control"
                                                       value="{{ request.user.username }}" disabled>
                                            </div>
                                            <div class="form-group">
                                                <label for="old_password">原密码:</label>
                                                <input type="password" class="form-control" id="old_password"
                                                       username="old_password">
                                            </div>
                                            <div class="form-group">
                                                <label for="new_password">新密码:</label>
                                                <input type="password" class="form-control" id="new_password"
                                                       username="new_password">
                                            </div>
                                            <div class="form-group">
                                                <label for="confirm_password">原密码:</label>
                                                <input type="password" class="form-control" id="confirm_password"
                                                       username="confirm_password">
                                            </div>
                                            <br>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-default" data-dismiss="modal">
                                                    取消
                                                </button>
                                                <button type="button" class="btn btn-primary" id="id_commit">保存</button>
                                                <span style="color: red" id="error"></span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                {% else %}
                    <li><a href="/user/register/">注册</a></li>
                    <li><a href="/user/login/">登录</a></li>
                {% endif %}
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>
<br>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-3">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title text-center">最新发布文章</h3>
                </div>
                <div class="panel-body">
                    {% for recent_article_obj in recent_article_list %}
                        {{ forloop.counter }}  <a href="/home/{{ recent_article_obj.blog.user.username }}/article/{{ recent_article_obj.id }}/">{{ recent_article_obj.title }}</a>
                        <br>
                        <br>
                    {% endfor %}
                </div>
            </div>

            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title text-center">阅读排行榜</h3>
                </div>
                <div class="panel-body">
                    {% for article in top_article_obj %}
                        {{ forloop.counter }}  <a href="/home/{{ article.blog.user.username }}/article/{{ article.id }}">{{ article.title }}</a>
                        &nbsp;<span class="glyphicon glyphicon-eye-open" style="color: #adadad">&nbsp{{ article.visitor_num }}</span>
                        <br>
                        <br>
                    {% endfor %}

                </div>
            </div>

        </div>
        <div class="col-md-9">
            <div class="panel panel-default">
                <div class="panel-body">
                    <h1 class="text-center">{{ article_obj.title }}</h1>
                    {% load static %}

                    <div class="text-center">
                        <span><img class="article-type-img" src="{% static  article_obj.type_link %}"
                                   alt="">&nbsp</span>
                        <span><a href="/mysite/{{ article_obj.blog.user.username }}/">{{ article_obj.blog.user.username }}</a>&nbsp</span>
                        <span style="color: #adadad">发布时间: &nbsp{{ article_obj.create_time|date:'Y-m-d H:i:s' }}&nbsp&nbsp</span>
                        <span class="glyphicon glyphicon-eye-open"
                              style="color: #adadad">&nbsp{{ article_visitor_num }}</span>
                    </div>
                    <div class="text-center">
                        文章标签:&nbsp
                        {% for tag_obj in article_obj.tags.all %}
                            <span style="color: #337FE5">{{ tag_obj.name }}&nbsp</span>
                        {% endfor %}
                    </div>
                    <hr>
                    <div style="overflow: hidden">
                        {{ article_obj.content|safe }}
                    </div>

                    <div class="clearfix">
                        <div id="div_digg">
                            <div class="diggit action">
                                <span class="diggnum" id="digg_count">{{ article_obj.up_num }}</span>
                            </div>
                            <div class="buryit action">
                                <span class="burynum" id="bury_count">{{ article_obj.down_num }}</span>
                            </div>
                            <div class="clear"></div>
                            <div class="diggword" id="digg_tips" style="color: red;"></div>
                        </div>
                    </div>
                    {#    评论楼渲染开始#}
                    <div>
                        <ul class="list-group">
                            {% for comment in comment_list %}
                                <li class="list-group-item">
                                    <span>#{{ forloop.counter }}楼</span>
                                    <span>{{ comment.create_time|date:'Y-m-d' }}</span>
                                    <span>{{ comment.user.username }}</span>
                                    <span><a class="pull-right reply" username="{{ comment.user.username }}"
                                             comment_id="{{ comment.pk }}">回复</a></span>
                                    <div>
                                        {#                            判断当前评论是否为子评论，如果是，则需要渲染对应父评论的用户名#}
                                        {% if comment.parent_id %}
                                            <p>@{{ comment.parent.user.username }}</p>
                                        {% endif %}
                                        {{ comment.content }}
                                    </div>
                                </li>
                            {% endfor %}
                        </ul>

                    </div>
                    {#    评论楼渲染结束#}
                    {#    文章评论样式开始#}
                    {% if request.user.is_authenticated %}  <!--登陆后才显示评论框-->
                        <hr>
                        <div>
                            <p><span class="glyphicon glyphicon-comment">发表评论</span></p>
                            <div>
                                <textarea name="comment" id="id_comment" cols="60" rows="10"></textarea>
                            </div>
                            <button class="btn btn-primary" id="id_submit">提交评论</button>
                            <span style="color:red;" id="errors"></span>
                        </div>
                    {% else %}
                        <hr>
                        <p>登陆后才能查看或发表评论，立即<a href="/user/login/">登陆</a>或者<a href="/user/register/">注册</a></p>
                    {% endif %}

                    {#    文章评论样式结束#}
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    //修改密码js
    $('#id_commit').click(function () {
        $.ajax({
            url: '/user/set_password/',
            type: 'post',
            data: {
                "username": $('#username').val(),
                "old_password": $('#old_password').val(),
                "new_password": $('#new_password').val(),
                "confirm_password": $('#confirm_password').val(),
                "csrfmiddlewaretoken": '{{ csrf_token }}'
            },
            success: function (args) {
                if (args.code === 1000) {
                    window.location.reload()
                } else {
                    $('#error').text(args.msg)
                }
            }
        })
    })

    //点赞点踩js
    $('.action').click(function () {
        let isup = $(this).hasClass('diggit');
        let $div = $(this)

        $.ajax({
            url: '/home/up_or_down/',
            type: 'post',
            data: {
                'article_id': {{article_obj.id}},
                'is_up': isup,
                'csrfmiddlewaretoken': '{{ csrf_token }}'
            },
            success: function (args) {
                if (args.code === 1000) {
                    $('#digg_tips').text(args.msg)
                    //点赞或点踩数量加1，先拿到原有的数字
                    let oldNum = $div.children().text()
                    $div.children().text(Number(oldNum) + 1)
                } else {
                    $('#digg_tips').html(args.msg)
                }
            }
        })
    })

    //评论js
    //在事件外部设置一个全局的parentId，则内部直接引用，不需要再let声明
    let parentId = null;
            $('#id_submit').click(function () {
            // 获取用户评论
            let conTent = $('#id_comment').val()
            // 判断当前评论是否为子评论，如果是，则需要将@usernmae字符去掉
            if(parentId){
                // 找到'\n'对应的索引，然后利用切片，切片是顾头不顾尾，所以需要+1
                let indexNum = conTent.indexOf('\n') + 1;
                conTent = conTent.slice(indexNum)
            }
            $.ajax({
                url: '/home/comment/',
                type: 'post',
                data: {
                    'article_id': '{{ article_obj.id }}',
                    'content': conTent,
                    // 如果parentId没有值，则是根评论，即为null，后端存储null是没有影响的
                    'parent_id':parentId,
                    'csrfmiddlewaretoken': '{{ csrf_token }}'
                },
                success: function (args) {
                    {#alert(args)#}
                    if (args.code === 1000) {
                        // 将评论框内容清空
                        $('#id_comment').val('')

                        //临时渲染评论楼
                        let userName = '{{ request.user.username }}'
                        let temp = `
		                            <li>
		                                <span>${userName}</span>
		                                <span><a href="#" class="pull-right">回复</a></span>
		                                <div>
		                                    ${conTent}
		                                </div>
		                            </li>
		                        `
                        // 将生成好的标签添加到ul标签内
                        $('.list-group').append(temp)
                        // 清空全局的parentId，否则没刷新页面，parentId不为空，后面所有的评论都是子评论
                        parentId = null;
                    }else {
                        $('#error').text(args.msg)
                    }
                }
            })
        })
        // 给回复按钮绑定点击事件
        $('.reply').click(function (){
            // 获取用户名
            let commentUserName = $(this).attr('username');
            // 获取主键值 直接修改全局
            parentId = $(this).attr('comment_id');

            // 拼接信息给评论框
            $('#id_comment').val('@' + commentUserName + '\n').focus()
        })

</script>
</body>
</html>